from src.db import db_models
from fastapi import FastAPI, APIRouter, Query, HTTPException, Path


# 学生就业管理模块
# 按照学生编号查询就业信息

async def get_job_by_info(sid: int):
    data = await db_models.Job.filter(stu_id=sid).first()
    if not data:
        raise HTTPException(status_code=404, detail=f'学生编号 {sid} 不存在')
    return data


# 按照公司名字查询就业信息

async def get_job_by_name(com_name: str):
    return await db_models.Job.filter(company_name__icontains=com_name).values()


# 按照工资范围查询就业信息

async def get_sal_range(min_sal: int = Query(..., gt=0, description="最低工资"),
                        max_sal: int = Query(..., gt=0, description="最高工资")):
    return await db_models.Job.filter(salary__gte=min_sal, salary__lte=max_sal).order_by('-salary')


# 定义基类


# 修改就业信息——增加新的

async def add_job(job):
    job = await db_models.Job.create(stu_id_id=job.stu_id_id,
                                     open_time=job.open_time,
                                     offer_time=job.offer_time,
                                     company_name=job.company_name,
                                     salary=job.salary)
    # await job.save()
    return {'添加成功': job}


# 修改就业信息——修改原有的

async def update_job(job_id, job):
    data = job.dict(exclude_unset=True)
    # data.pop('stu_id')
    a = await db_models.Job.filter(stu_id=job.stu_id_id, id=job_id).update(**data)
    if a:
        return {'提示': f'成功更新id为{job_id}编号为{job.stu_id_id}的学生就业信息!'}
    else:
        return {"提示": "该记录不存在"}


# 删除就业信息

async def delete_job_info(sid: int):
    delete_count = await db_models.Job.filter(stu_id=sid).delete()
    if not delete_count:
        raise HTTPException(status_code=404, detail=f'要删除的{sid}不存在')
    return {'提示': f'成功删除编号为{sid}的学生就业信息!'}


